﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MSIACCSL2.ViewModels;
using WcfService1.DTO;

namespace MSIACCSL2.Views.FormSearch
{
    public partial class InventorySearch : ChildWindow
    {
        
        public SearchInventoryViewModel ViewModel
        {
            get
            {
                return this.DataContext as SearchInventoryViewModel;
            }
        }

        public InventoryDTO Inventory { get; set; }

        public Type Pemanggil { get; set; }

        private bool _isInitialLoad = true;
        public string SerialNumber;
        private int CurrentIndex = 0;
        public InventorySearch()
        {
            InitializeComponent();
            ViewModel.Inventory.Item = new ItemDTO();
          
            //dg1.KeyUp +=new KeyEventHandler(dg1_KeyUp);
            dg1.SelectionChanged += new EventHandler(dg1_SelectionChanged);
          
          

            btnSelect.Click += (o, e) =>
                                   {
                                       this.Close();
                                   };
            this.GotFocus += new RoutedEventHandler(InventorySearch_GotFocus);
            this.Loaded += new RoutedEventHandler(InventorySearch_Loaded);
            this.KeyDown += new KeyEventHandler(InventorySearch_KeyDown);
        }

       
        void dg1_SelectionChanged(object sender, EventArgs e)
        {
            GetSelectedItems();
        }

       

        void InventorySearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F && Keyboard.Modifiers == ModifierKeys.Control)
            {
                ViewModel.Expression = string.Empty;
                if (this.rblOr.IsChecked ?? false) ViewModel.Expression = "OR";
                else ViewModel.Expression = "AND";
                ViewModel.SearchInventory(ViewModel.Inventory);
                dg1.Focus(); 
            }
          
        }

        void InventorySearch_Loaded(object sender, RoutedEventArgs e)
        {
            _isInitialLoad = true;
            if (Pemanggil != null)
            {
                if (Pemanggil == typeof(SpkViewModel)) 
                    ViewModel.Inventory.IsStock = false;
            }
        }

        void InventorySearch_GotFocus(object sender, RoutedEventArgs e)
        {
            if (_isInitialLoad)
            {
                _isInitialLoad = false;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.Expression = string.Empty;
            if (this.rblOr.IsChecked ?? false) ViewModel.Expression = "OR";
            else ViewModel.Expression = "AND";
            ViewModel.SearchInventory(ViewModel.Inventory);
            dg1.Focus();
        }

   
        private void CancelButton_Click_1(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void GetSelectedItems()
        {
            if (dg1.ItemsSource == null || dg1.SelectedRanges.Count == 0)
                return;
            IAsyncResult result = dg1.BeginGetSelectedItems(new AsyncCallback(this.ProcessSelectedItems), null);
           
            if (result.IsCompleted)
            {
                Inventory = (InventoryDTO)dg1.EndGetSelectedItems(result).FirstOrDefault();
                ViewModel.SerialNumber = ((InventoryDTO)Inventory).SerialNumber;
                SerialNumber = ((InventoryDTO)Inventory).SerialNumber;
            }
        }

        private void ProcessSelectedItems(IAsyncResult result)
        {
            if (result.CompletedSynchronously)
                return;
            IEnumerable<object> selectedItems = dg1.EndGetSelectedItems(result);
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (ViewModel.GridItemVisibility != Visibility.Collapsed)
                ViewModel.GridItemVisibility = Visibility.Collapsed;
        }

        private void btnSelectSubgroup_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (ViewModel.GridSubgroupVisibility != Visibility.Collapsed)
                ViewModel.GridSubgroupVisibility = Visibility.Collapsed;
        }

        private void cboWarehouse_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ViewModel.WarehouseSelected = (sender as ComboBox).SelectedItem as WarehouseDTO;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            ViewModel.Pallet = (sender as TextBox).Text;
        }



    }
}



